package com.gongpingjialoan.activity.tool;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.activity.search.CityActivity;
import com.gongpingjialoan.data.CityData;
import com.gongpingjialoan.global.GPJApplication;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.widget.LoadingDialog;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 二手车准入标准查询
 * 
 * @author Administrator
 * 
 */
public class AccessToCityActivity extends BaseActivity
{
    private AccessToCityActivity mySelf = this;
    
    private String city;
    
    private TextView cityPick;
    
    private Button query;
    
    private TextView cityView;
    
    private TextView dvmView;
    
    private TextView emissionView;
    
    private TextView detialView;
    
    private Button backImg;
    
    // private ProgressDialog progressDialog;
    private LoadingDialog loadingDialog;
    
    private static final int REQUEST_CITY = 0;
    
    private RelativeLayout city_layout;
    
    private CityData mCityData;
    
    View result_layoutV;
    
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_access_to_city);
        
        initBase(mySelf);
        setTitle("准入标准查询");
        
        mCityData = ((GPJApplication)getApplication()).getCityData();
        
        cityPick = (TextView)findViewById(R.id.city_pick);
        cityPick.setText(mCityData.mCurrentCity);
        city = mCityData.mCurrentCity;
        query = (Button)findViewById(R.id.query);
        cityView = (TextView)findViewById(R.id.city);
        dvmView = (TextView)findViewById(R.id.dvm);
        emissionView = (TextView)findViewById(R.id.emisson_standard);
        detialView = (TextView)findViewById(R.id.standard_details);
        city_layout = (RelativeLayout)findViewById(R.id.city_layout);
        result_layoutV = findViewById(R.id.result_layout);
        
        if (!city.equals(""))
        {
            loadInfo();
        }
        
        setQueryListener();
        
        setCityPickListener();
        
    }
    
    private void setCityPickListener()
    {
        city_layout.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent();
                intent.setClass(AccessToCityActivity.this, CityActivity.class);
                startActivityForResult(intent, REQUEST_CITY);
            }
            
        });
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == REQUEST_CITY) {
                city = data.getExtras().getString("city");
                cityPick.setText(city);
            }
        }
    }
    
    private void setQueryListener()
    {
        query.setOnClickListener(new OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                loadInfo();
            }
            
        });
    }
    
    public void loadInfo()
    {
        String url = "/mobile/more/access-standard/";
//        String para = "?city=" + city;
        // progressDialog = ProgressDialog.show(AccessToCityActivity.this,"请稍等...", "正在提交请求...", true);
        loadingDialog = new LoadingDialog(this);
        loadingDialog.show();
        NetDataJson mNetDataJson = new NetDataJson(new NetDataJson.OnNetDataJsonListener()
        {
            @Override
            public void onDataJsonError(String errorMessage)
            {
                // progressDialog.dismiss();
                loadingDialog.dismiss();
                // Toast.makeText(getApplicationContext(),errorMessage, Toast.LENGTH_SHORT).show();
                showTips(4, errorMessage);
                result_layoutV.setVisibility(View.GONE);
            }
            
            @Override
            public void onDataJsonUpdate(JSONObject json)
            {
                try
                {
                    // progressDialog.dismiss();
                    loadingDialog.dismiss();
                    result_layoutV.setVisibility(View.VISIBLE);
                    JSONObject standard = json.getJSONObject("standard");
                    cityView.setText(standard.getString("city"));
                    dvmView.setText(standard.getString("DVM"));
                    detialView.setText(standard.getString("standard_details"));   
                    emissionView.setText(standard.getString("emission_standard"));
                }
                catch (JSONException e)
                {
                    e.printStackTrace();
                }
                
            }
            
        });
        mNetDataJson.addParam("city",city);
        mNetDataJson.setUrl(url);
        mNetDataJson.request("get");
    }
    
}
